package ac.github.os.listener

import ac.github.os.api.OriginSkillAPI
import ac.github.os.internal.core.condition.impl.entity.damage.DamageEntityCause
import ac.github.os.internal.core.holder.damage.DamageEntityHolder
import ac.github.os.internal.core.holder.entity.InteractHolder
import ac.github.os.internal.core.holder.player.*
import ac.github.os.internal.core.holder.plugin.RunnableHolder
import ac.github.os.internal.core.trigger.PlayerTrigger
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.entity.Projectile
import org.bukkit.event.block.Action
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.player.*
import org.bukkit.inventory.EquipmentSlot
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.event.EventPriority
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.info
import taboolib.common.platform.function.submit
import taboolib.platform.util.isNotAir

object CallListener {


    @Awake(LifeCycle.ENABLE)
    fun runnable() {
        submit(async = true, period = 20) {
            Bukkit.getOnlinePlayers().forEach {
                OriginSkillAPI.cast(PlayerTrigger(it), RunnableHolder())
            }
        }
    }

    @SubscribeEvent
    fun e(e: PlayerJoinEvent) {
        OriginSkillAPI.cast(PlayerTrigger(e.player), PlayerJoinHolder(e))
    }

    @SubscribeEvent(
        priority = EventPriority.MONITOR
    )
    fun e(e: EntityDamageByEntityEvent) {
        val damageEntityHolder = DamageEntityHolder(e)
        var damager: LivingEntity = damageEntityHolder.damager

        if (damager is Projectile) {
            damager = damager.shooter as LivingEntity
        }

        if (damager is Player) {
            OriginSkillAPI.cast(PlayerTrigger(damager), damageEntityHolder)
        }

        val entity = damageEntityHolder.entity
        if (entity is Player) {
            OriginSkillAPI.cast(PlayerTrigger(entity), damageEntityHolder)
        }
    }

    @SubscribeEvent
    fun e(e: PlayerInteractEntityEvent) {
        val rightClicked = e.rightClicked
        if (rightClicked is LivingEntity) {
            OriginSkillAPI.cast(PlayerTrigger(e.player), PlayerInteractEntityHolder(e))
        }
    }

    @SubscribeEvent
    fun e(e: PlayerInteractEvent) {
        OriginSkillAPI.cast(PlayerTrigger(e.player), InteractHolder(e))
    }

    @SubscribeEvent
    fun e(e: PlayerQuitEvent) {
        OriginSkillAPI.cast(PlayerTrigger(e.player), PlayerQuitHolder(e))
    }

    @SubscribeEvent
    fun e(e: PlayerTeleportEvent) {
        OriginSkillAPI.cast(PlayerTrigger(e.player), PlayerTeleportHolder(e))
    }

    @SubscribeEvent
    fun e(e: AsyncPlayerChatEvent) {
        OriginSkillAPI.cast(PlayerTrigger(e.player), PlayerChatHolder(e))
    }

    /**
     * 点击背包
     */
    @SubscribeEvent
    fun e(e: InventoryClickEvent) {
        val entity = e.whoClicked
        val inventoryView = e.view
        if (inventoryView.title === "container.crafting") {
            // 点击背包格子
            OriginSkillAPI.cast(PlayerTrigger(entity as Player), PlayerClickKnapsackHolder(e))
        } else {
            OriginSkillAPI.cast(PlayerTrigger(entity as Player), PlayerClickInventory(e))
        }
    }

}